package com.hopedove.returnyarnsystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hopedove.base.BaseActivity;
import com.hopedove.net.ErrResponse;
import com.hopedove.net.OkResponse;
import com.hopedove.net.domain.Communication;
import com.hopedove.net.domain.GetReturnYarnInfo;
import com.hopedove.returnyarnsystem.R;
import com.hopedove.returnyarnsystem.constants.Constants;
import com.hopedove.utils.ToastUtil;
import com.hopedove.view.TipsDialogFragment;

public class InputJTActivity extends BaseActivity implements OnClickListener {

    public static String SOURCE_DATA = "source_data";
    private final int MAX_4 = 4;
    private EditText barCode;
    private TipsDialogFragment tipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inputjtcode);
        barCode = findViewById(R.id.barcode_et);
        barCode.setText("");
        findViewById(R.id.confirm_barcode_img).setOnClickListener(this);

        // 控制输入框只能接受4为数字，超过9位后，自动覆盖
        barCode.addTextChangedListener(new TextWatcher() {
            private int editEnd;
            private CharSequence temp;

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                editEnd = barCode.getSelectionEnd();
                if (temp.length() > MAX_4) {
                    s.delete(0, editEnd - 1);
                    barCode.setText(s);
                    barCode.setSelection(editEnd + 1);
                }

            }
        });

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.confirm_barcode_img) {

            String jtNo = barCode.getText().toString().trim();
            if (TextUtils.isEmpty(jtNo)) {
                ToastUtil.longToast(this, "请先输入机台");
                return;
            }

            View view = getLayoutInflater().inflate(R.layout.loading, null);
            tipDialog = TipsDialogFragment.newInstance(view);
            TextView textView = view.findViewById(R.id.tip);
            String tipStr = "正在获取信息...";
            textView.setText(tipStr);
            tipDialog.setCancelable(false);
            tipDialog.show(getSupportFragmentManager(), "");

            getReturnYarnInfo();
        }
    }

    @Override
    protected void onResume() {
        barCode.setText("");
        super.onResume();
    }

    /**
     * 获取纱单详情信息
     */
    void getReturnYarnInfo() {

        GetReturnYarnInfo getReturnYarnInfo = new GetReturnYarnInfo(barCode.getText().toString());
        Communication.asyncRequest(getReturnYarnInfo, (r) -> {
            TipsDialogFragment.dismissTipDialog(tipDialog);

            if (r instanceof OkResponse) {
                String data = ((OkResponse) r).getData();
                Intent intent = new Intent(InputJTActivity.this,
                        MaterialInfoActivity.class);
                intent.putExtra(Constants.JT_NO, barCode.getText().toString());
                intent.putExtra(SOURCE_DATA, data);
                startActivity(intent);
            } else if (r instanceof ErrResponse) {
                barCode.setText(null);
                ToastUtil.longToast(getBaseContext(), ((ErrResponse) r).getErrText());
            }
        });
    }


}
